package com.eastfair.auth.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.eastfair.core.base.entity.Entity;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import static com.eastfair.core.utils.DateUtils.DEFAULT_DATE_TIME_FORMAT;
import com.eastfair.annotation.model.EchoVO;
import com.eastfair.auth.enumeration.HttpMethod;
import com.eastfair.auth.enumeration.LogType;

import static com.baomidou.mybatisplus.annotation.SqlCondition.LIKE;

/**
 * <p>
 * 实体类
 * 业务日志记录
 * </p>
 *
 * @author ligang
 * @since 2023-02-13
 */
@Data
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("c_system_log")
@ApiModel(value = "SystemLog", description = "业务日志记录")
@AllArgsConstructor
public class SystemLog extends Entity<Long> implements EchoVO {

    private static final long serialVersionUID = 1L;
    @TableField(exist = false)
    private Map<String, Object> echoMap = new HashMap<>();
    /**
     * 请求IP
     */
    @ApiModelProperty(value = "请求IP")
    @Size(max = 255, message = "请求IP长度不能超过255")
    @TableField(value = "request_ip", condition = LIKE)
    @Excel(name = "请求IP")
    private String requestIp;

    /**
     * 后台处理IP
     */
    @ApiModelProperty(value = "后台处理IP")
    @Size(max = 255, message = "后台处理IP长度不能超过255")
    @TableField(value = "ip_addr", condition = LIKE)
    @Excel(name = "后台处理IP")
    private String ipAddr;

    /**
     * 链路追踪id
     */
    @ApiModelProperty(value = "链路追踪id")
    @Size(max = 255, message = "链路追踪id长度不能超过255")
    @TableField(value = "trace_id", condition = LIKE)
    @Excel(name = "链路追踪id")
    private String traceId;

    /**
     * 类型 #LogType{OPT:操作类型;EX:异常类型}
     */
    @ApiModelProperty(value = "类型 #LogType{OPT:操作类型;EX:异常类型}")
    @TableField(value = "type")
    @Excel(name = "类型 #LogType{OPT:操作类型;EX:异常类型}", replace = {"操作类型_OPT", "异常类型_EX",  "_null"})
    private LogType type;

    /**
     * 用户id
     */
    @ApiModelProperty(value = "用户id")
    @NotNull(message = "请填写用户id")
    @TableField(value = "user_id")
    @Excel(name = "用户id")
    private Long userId;

    /**
     * 操作人
     */
    @ApiModelProperty(value = "操作人")
    @Size(max = 255, message = "操作人长度不能超过255")
    @TableField(value = "user_name", condition = LIKE)
    @Excel(name = "操作人")
    private String userName;

    /**
     * 操作描述
     */
    @ApiModelProperty(value = "操作描述")
    @Size(max = 1255, message = "操作描述长度不能超过1255")
    @TableField(value = "description", condition = LIKE)
    @Excel(name = "操作描述")
    private String description;

    /**
     * 类路径
     */
    @ApiModelProperty(value = "类路径")
    @Size(max = 255, message = "类路径长度不能超过255")
    @TableField(value = "class_path", condition = LIKE)
    @Excel(name = "类路径")
    private String classPath;

    /**
     * 请求类型
     */
    @ApiModelProperty(value = "请求类型")
    @Size(max = 255, message = "请求类型长度不能超过255")
    @TableField(value = "action_method", condition = LIKE)
    @Excel(name = "请求类型")
    private String actionMethod;

    /**
     * 请求地址
     */
    @ApiModelProperty(value = "请求地址")
    @Size(max = 255, message = "请求地址长度不能超过255")
    @TableField(value = "request_uri", condition = LIKE)
    @Excel(name = "请求地址")
    private String requestUri;

    /**
     * 请求类型#HttpMethod{GET:GET请求;POST:POST请求;PUT:PUT请求;DELETE:DELETE请求;PATCH:PATCH请求;TRACE:TRACE请求;HEAD:HEAD请求;OPTIONS:OPTIONS请求;}
     */
    @ApiModelProperty(value = "请求类型#HttpMethod{GET:GET请求;POST:POST请求;PUT:PUT请求;DELETE:DELETE请求;PATCH:PATCH请求;TRACE:TRACE请求;HEAD:HEAD请求;OPTIONS:OPTIONS请求;}")
    @TableField(value = "http_method")
    @Excel(name = "请求类型#HttpMethod{GET:GET请求;POST:POST请求;PUT:PUT请求;DELETE:DELETE请求;PATCH:PATCH请求;TRACE:TRACE请求;HEAD:HEAD请求;OPTIONS:OPTIONS请求;}", replace = {"GET请求_GET", "POST请求_POST", "PUT请求_PUT", "DELETE请求_DELETE", "PATCH请求_PATCH", "TRACE请求_TRACE", "HEAD请求_HEAD", "OPTIONS请求_OPTIONS",  "_null"})
    private HttpMethod httpMethod;

    /**
     * 请求参数
     */
    @ApiModelProperty(value = "请求参数")
    @Size(max = 1255, message = "请求参数长度不能超过1255")
    @TableField(value = "params", condition = LIKE)
    @Excel(name = "请求参数")
    private String params;

    /**
     * 返回值
     */
    @ApiModelProperty(value = "返回值")
    @Size(max = 255, message = "返回值长度不能超过255")
    @TableField(value = "result", condition = LIKE)
    @Excel(name = "返回值")
    private String result;

    /**
     * 异常描述
     */
    @ApiModelProperty(value = "异常描述")
    @Size(max = 255, message = "异常描述长度不能超过255")
    @TableField(value = "ex_detail", condition = LIKE)
    @Excel(name = "异常描述")
    private String exDetail;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "start_time")
    @Excel(name = "创建时间", format = DEFAULT_DATE_TIME_FORMAT, width = 20)
    private LocalDateTime startTime;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "finish_time")
    @Excel(name = "创建时间", format = DEFAULT_DATE_TIME_FORMAT, width = 20)
    private LocalDateTime finishTime;

    /**
     * 消耗时间
     */
    @ApiModelProperty(value = "消耗时间")
    @TableField(value = "consuming_time")
    @Excel(name = "消耗时间")
    private Integer consumingTime;

    /**
     * 浏览器
     */
    @ApiModelProperty(value = "浏览器")
    @Size(max = 255, message = "浏览器长度不能超过255")
    @TableField(value = "ua", condition = LIKE)
    @Excel(name = "浏览器")
    private String ua;

    /**
     * 是否成功 1是 0否
     */
    @ApiModelProperty(value = "是否成功 1是 0否")
    @TableField(value = "is_success")
    @Excel(name = "是否成功 1是 0否")
    private Integer isSuccess;

    /**
     * 是否启用 1是 0否
     */
    @ApiModelProperty(value = "是否启用 1是 0否")
    @TableField(value = "is_enabled")
    @Excel(name = "是否启用 1是 0否")
    private Integer isEnabled;

    /**
     * 是否删除 1是 0否
     */
    @ApiModelProperty(value = "是否删除 1是 0否")
    @TableField(value = "is_deleted")
    @Excel(name = "是否删除 1是 0否")
    private Integer isDeleted;

    /**
     * 租户id
     */
    @ApiModelProperty(value = "租户id")
    @TableField(value = "tenant_id")
    @Excel(name = "租户id")
    private Long tenantId;

    /**
     * 项目id
     */
    @ApiModelProperty(value = "项目id")
    @TableField(value = "project_id")
    @Excel(name = "项目id")
    private Long projectId;

    /**
     * 子系统id
     */
    @ApiModelProperty(value = "子系统id")
    @TableField(value = "subsystem_id")
    @Excel(name = "子系统id")
    private Long subsystemId;

    /**
     * 团队id
     */
    @ApiModelProperty(value = "团队id")
    @TableField(value = "team_id")
    @Excel(name = "团队id")
    private Long teamId;


    @Builder
    public SystemLog(Long id, Long createdBy, LocalDateTime createTime, Long updatedBy, LocalDateTime updateTime, 
                    String requestIp, String ipAddr, String traceId, LogType type, Long userId, 
                    String userName, String description, String classPath, String actionMethod, String requestUri, HttpMethod httpMethod, 
                    String params, String result, String exDetail, LocalDateTime startTime, LocalDateTime finishTime, Integer consumingTime, 
                    String ua, Integer isSuccess, Integer isEnabled, Integer isDeleted, Long tenantId, Long projectId, 
                    Long subsystemId, Long teamId) {
        this.id = id;
        this.createdBy = createdBy;
        this.createTime = createTime;
        this.updatedBy = updatedBy;
        this.updateTime = updateTime;
        this.requestIp = requestIp;
        this.ipAddr = ipAddr;
        this.traceId = traceId;
        this.type = type;
        this.userId = userId;
        this.userName = userName;
        this.description = description;
        this.classPath = classPath;
        this.actionMethod = actionMethod;
        this.requestUri = requestUri;
        this.httpMethod = httpMethod;
        this.params = params;
        this.result = result;
        this.exDetail = exDetail;
        this.startTime = startTime;
        this.finishTime = finishTime;
        this.consumingTime = consumingTime;
        this.ua = ua;
        this.isSuccess = isSuccess;
        this.isEnabled = isEnabled;
        this.isDeleted = isDeleted;
        this.tenantId = tenantId;
        this.projectId = projectId;
        this.subsystemId = subsystemId;
        this.teamId = teamId;
    }

}
